package io.renren.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.config.BaseConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author create by jieshao email:2609207540@qq.com
 * @projectName city-app-api
 * @Description: 创蓝验证码发送
 * @date 2019-11-25
 */
@Component
@Slf4j
public class CLMessageSendComponent {

    @Resource
    private BaseConstant baseConstant;

    public static void main(String[] args) {
        //短信下发
        String sendUrl = "http://smssh1.253.com/msg/send/json";
        Map map = new HashMap();
        map.put("account","N4063557");//API账号
        map.put("password","8Kqw2P9oItc311");//API密码
        map.put("msg","【集讯云】您好，您的验证码是{d6},请在五分钟内输入。为保障您的账号安全，请勿将验证码短信转发给他人");//短信内容
        map.put("phone","19950523112");//手机号
        map.put("report","true");//是否需要状态报告
        map.put("extend","");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
//        System.out.println(sendSmsByPost(sendUrl,js.toString()));
//        String resultStr = sendSmsByPost(sendUrl, js.toString());
//        Map<String,String> resultMap = (Map<String, String>) JSON.parse(resultStr);

        //查询余额
        String balanceUrl = "https://xxx/msg/balance/json";
        Map map1 = new HashMap();
        map1.put("account","YZM5552052");
        map1.put("password","XH5wVAh6bmfe82");
        JSONObject js1 = (JSONObject) JSONObject.toJSON(map1);
//        System.out.println(sendSmsByPost(balanceUrl,js1.toString()));
    }

    /**
      * @description: 发送手机验证码
      * @auther: create by jieShao email:2609207540@qq.com
      * @date 2019/11/25
      * @param mobile 手机号
      * @param code 验证码
      * @return
      */
    public Map<String,String> ClSendMeaasge(String mobile, String code){
        //封装请求参数
        Map map = new HashMap();
        map.put("account","N4063557");//API账号
        map.put("password","8Kqw2P9oItc311");//API密码
        map.put("msg","【酒都建设】您好，您的验证码是: "+code+" ,请在五分钟内输入。为保障您的账号安全，请勿将验证码短信转发给他人");//短信内容
        map.put("phone",mobile);//手机号
        map.put("report","true");//是否需要状态报告
        map.put("extend","");//自定义扩展码
        JSONObject js = (JSONObject) JSONObject.toJSON(map);
        String resultStr = sendSmsByPost(baseConstant.getClSendMessageUrl(), js.toString());
        log.info("=============[创蓝]发送短信验证码返回结果{}",resultStr);
        Map<String,String> resultMap = (Map<String, String>) JSON.parse(resultStr);
        return resultMap;
    }



    /**
      * @description: 发送请求
      * @auther: create by jieShao email:2609207540@qq.com
      * @date 2019/11/25
      * @param path
      * @return
      */
    public String sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            StringBuilder sb = new StringBuilder();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
